﻿using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Windows.Forms;

namespace TcpApplication{
    public class Server{
        public static void BuildServer(Control status){
             
            IPAddress ipAd = IPAddress.Parse("127.0.0.1");
            TcpListener myList = new TcpListener(ipAd, 8001);
            myList.Start();
            status.Text += "[本地服务端口为: 8001]" + Environment.NewLine;
            status.Text += "[本地套接字为: " + myList.LocalEndpoint + "]" + Environment.NewLine;
            status.Text += "[等待连接]" + Environment.NewLine;
            Socket s = myList.AcceptSocket();
            status.Text += "[对方套接字为：" + s.RemoteEndPoint + "]"  + Environment.NewLine;
            byte[] b = new byte[100];
            int k = s.Receive(b);
            status.Text += "[开始接收数据]" + Environment.NewLine;
            for (int i = 0; i < k; i++) {
                status.Text += Convert.ToChar(b[i]);
            }
            ASCIIEncoding asen = new ASCIIEncoding();
            s.Send(asen.GetBytes("The string was received by the server."));
            status.Text += Environment.NewLine;
            s.Close();
            myList.Stop();
        }
    }
}